package com.stylefeng.guns.rest.modular.attent.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.stylefeng.guns.rest.common.persistence.dao.CareMapper;
import com.stylefeng.guns.rest.common.persistence.model.*;
import com.stylefeng.guns.rest.common.utils.jpush.JpushUtils;
import com.stylefeng.guns.rest.modular.artitle.service.ArticleService;
import com.stylefeng.guns.rest.modular.attent.controller.dto.AttentUserRequest;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleListResponse;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.ArticleRequest;
import com.stylefeng.guns.rest.modular.catalogy.controller.dto.CatalogyListRequest;
import com.stylefeng.guns.rest.modular.content.controller.dto.MyCommentList;
import com.stylefeng.guns.rest.modular.message.service.MessageService;
import com.stylefeng.guns.rest.modular.users.service.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS-PC on 2018/9/8.
 */
@Service
public class CareService {

    @Autowired
    CareMapper careMapper;
    @Autowired
    private ArticleService articleService;
    @Autowired
    UserService userService;
    @Autowired
    MessageService messageService;

    //插入点赞
    public boolean insertCare(ArticleRequest request){
        Article a= articleService.queryArtitle(request.getArticleId());
        CareModel careModel = new CareModel();
        careModel.setUserId(request.getUserId());
        careModel.setTitleUser(a.getUserId());
        careModel.setArtitleId(request.getArticleId());
        careModel.setCreateTime(new Date());

        //插入消息
        Message message = new Message();
        message.setType(3);
        message.setyUser(a.getUserId());
        message.setcUser(request.getUserId());
        message.setCreateTime(new Date());
        messageService.inserData(message);

        //发送极光推送通知
        User sendUser = userService.selectUserByUserId(a.getUserId());
        User zanUser = userService.selectUserByUserId(request.getUserId());//点赞用户id
        JpushUtils.sendPushMessageByAlias(sendUser.getPhone(),zanUser.getNickName(),3);

        return careMapper.insert(careModel)>0?true:false;
    }

    //取消点赞
    public boolean deleteAttent(ArticleRequest request){
        List<CareModel> lists = careMapper.selectList(Condition.create().eq("artitle_id",request.getArticleId())
                .eq("user_id",request.getUserId()));
        if(lists.size()==0){
            return false;
        }
        return careMapper.delete(Condition.create().eq("artitle_id",request.getArticleId())
                .eq("user_id",request.getUserId()))>0?true:false;
    }

    //查询某个用户是否被关注了
    public int queryCareByUser(int userId,int artitleId){
        List<CareModel> lists = careMapper.selectList(Condition.create().eq("artitle_id",artitleId)
                .eq("user_id",userId));
        if(lists.size()==0){
            return 0;
        }else {
            return 1;
        }
    }
    //查询文章所有的点赞数量
    public int queryAllCareByUser(int artitleId){
        return careMapper.selectList(Condition.create().eq("artitle_id",artitleId)).size();
    }
    //查询用户所有的点赞数量
    public int queryAllCareByUserId(int userId){
//        return careMapper.selectList(Condition.create().eq("user_id",userId)).size();
        Map<String ,Object> data = new HashedMap();
        data.put("userId",userId);
        return careMapper.getUserAllCareNums(data);
    }


    public List<ArticleListResponse> artitleCareList(CatalogyListRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex",(request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return careMapper.artitleCareList(data);
    }

    //获取别人给我文章的点赞
    public List<MyCommentList> getMyCareList(CatalogyListRequest request){
        Map<String ,Object> data = new HashedMap();
        data.put("userId",request.getUserId());
        data.put("pageIndex",(request.getPage()-1)*request.getSize());
        data.put("pageSize",request.getSize());
        return careMapper.getMyCaretList(data);
    }

    //删除视频所有的点赞
    public void deleteAllCares(int artitleId){
        careMapper.delete(Condition.create().eq("artitle_id",artitleId));
    }
}
